
//
//  EnvironmentChartTableViewCell.m
//  Rail
//
//  Created by hychen on 2018/3/6.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "EnvironmentChartTableViewCell.h"
#import "SCChart.h"

@interface EnvironmentChartTableViewCell ()<SCChartDataSource>
@property (nonatomic, strong) UIImageView *lineImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *chartContainer;
@property (nonatomic, strong) BMLineView *lineView;

@property (nonatomic, strong) SCChart *chart;

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *timeArray;
@end
@implementation EnvironmentChartTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupTlSubviews];
    }
    return self;
}

- (void)setupTlSubviews {
    self.lineView = [BMLineView defaultLineView];
    [self.contentView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(10);
    }];
    
    self.lineImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.lineImageView];
    
    self.titleLabel = [UILabel labelWithText:@"" textColor:Color_Hex(0x565656) font:[UIFont systemFontOfSize:14.f]];
    [self.contentView addSubview:self.titleLabel];
    
    [self.lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(2);
        make.height.mas_equalTo(12);
        make.left.top.mas_equalTo(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineImageView.mas_right).offset(5);
        make.centerY.equalTo(self.lineImageView);
    }];
    
    self.chartContainer = [UIView new];
    [self.contentView addSubview:self.chartContainer];
    [self.chartContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.lineView.mas_top);
    }];
    
}

- (void)setModel:(EnvironmentChartModel *)model {
    _model = model;
    self.titleLabel.text = model.title;
    self.timeArray = model.timeArray;
    self.dataArray = model.dataArray;
    [self configChart];
}

- (void)configChart {
    if (self.chart.superview) {
        [self.chart removeFromSuperview];
        self.chart = nil;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.chart = [[SCChart alloc] initwithSCChartDataFrame:self.chartContainer.bounds
                                                    withSource:self
                                                     withStyle:SCChartLineStyle];
        [self.chart showInView:self.chartContainer];
        [self.chart strokeChart];
    });

}

#pragma mark - @required
//横坐标标题数组
- (NSArray *)SCChart_xLableArray:(SCChart *)chart {
    return self.timeArray;
}

//数字数组
- (NSArray *)SCChart_yValueArray:(SCChart *)chart {
    return @[self.dataArray];
}

#pragma mark - @optional
//颜色数组
- (NSArray *)SCChart_ColorArray:(SCChart *)chart {
    return @[Color_RGB(0, 193, 224)];
}

#pragma mark 折线图专享功能
//标记数值区域
- (CGRange)SCChartMarkRangeInLineChart:(SCChart *)chart {
    return CGRangeZero;
}

//判断显示横线条
- (BOOL)SCChart:(SCChart *)chart ShowHorizonLineAtIndex:(NSInteger)index {
    return YES;
}

//判断显示最大最小值
- (BOOL)SCChart:(SCChart *)chart ShowMaxMinAtIndex:(NSInteger)index {
    return NO;
}
@end

